import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Administrator
 * Date: 2023-09-06
 * Time: 10:02
 */
public class Test {
    public static int dayOfYear(String date) {
        int year = Integer.parseInt(date.substring(0, 4));
        int month = Integer.parseInt(date.substring(5, 7));
        int day = Integer.parseInt(date.substring(8));

        int[] amount = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
        if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)) {
            amount[1] = 29;
        }

        int ans = 0;
        for (int i = 0; i < month - 1; ++i) {
            ans += amount[i];
        }
        return ans + day;
    }
    public static int dayOfYear1(String date){
        int year = Integer.parseInt(date.substring(0,4));
        int month = Integer.parseInt(date.substring(5,7));
        int day = Integer.parseInt(date.substring(8));
        int[] monday = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
        if((year % 100 != 0) && (year % 4 == 0) || (year % 400 == 0)){
            monday[1] = 29;
        }
        int mondy = 0;
        for (int i = 0; i < month - 1; ++i) {
            mondy += monday[i];
        }
        return mondy+day;
    }
    public static void main(String[] args) {
        String date = "1999-02-10";
        int ret = dayOfYear1(date);
        System.out.println(ret);
    }

    public int countCharacters1(String[] words, String chars) {
        Map<Character, Integer> charsCnt = new HashMap<>();
        int length = chars.length();
        for (int i = 0; i < length; ++i) {
            char c = chars.charAt(i);
            charsCnt.put(c, charsCnt.getOrDefault(c, 0) + 1);
        }
        int ans = 0;
        for (String word : words) {
            Map<Character, Integer> wordCnt = new HashMap<>();
            int wordLength = word.length();
            for (int i = 0; i < wordLength; ++i) {
                char c = word.charAt(i);
                wordCnt.put(c, wordCnt.getOrDefault(c, 0) + 1);
            }
            boolean isAns = true;
            for (int i = 0; i < wordLength; ++i) {
                char c = word.charAt(i);
                if (charsCnt.getOrDefault(c, 0) < wordCnt.getOrDefault(c, 0)) {
                    isAns = false;
                    break;
                }
            }
            if (isAns) {
                ans += word.length();
            }
        }
        return ans;
    }

    public static int countCharacters(String[] words, String chars) {
        int[] c = new int[26];
        for(char cc : chars.toCharArray()) {
            c[(int)(cc - 'a')] += 1;
        }
        int res = 0;
        a: for(String word : words) {
            int[] w = new int[26];
            for(char ww : word.toCharArray()) {
                w[(int)(ww - 'a')] += 1;
            }
            for(int i=0; i<26; i++) {
                if(w[i] > c[i]) {
                    continue a;
                }
            }
            res += word.length();
        }
        return res;
    }

    public static int countCharacters2(String[] words, String chars){
        int[] c = new int[26];
        for (char cc: chars.toCharArray()) {
            c[(int)(cc-'a')] += 1;
        }

        int res = 0;
        a:for (String word: words) {
            int[] w = new int[26];
            for (char ww : word.toCharArray()) {
                w[(int)(ww - 'a')] += 1;
            }
            for (int i = 0; i < 26; i++) {
                if(w[i] > c[i]){
                    continue a;
                }
            }
            res += word.length();
        }
        return res;
    }
    public static int countCharacters3(String[] words, String chars){
        int ans = 0;
        Map<Character,Integer> charCnt = new HashMap<>();
        int charlen = chars.length();
        for (int i = 0; i < charlen; i++) {
            char c = chars.charAt(i);
            charCnt.put(c,charCnt.getOrDefault(c,0) +1);
        }
        for (String word: words) {
            Map<Character,Integer> wordCnt = new HashMap<>();
            int wordlen = word.length();
            for (int i = 0; i < wordlen; i++) {
                char c = word.charAt(i);
                wordCnt.put(c,wordCnt.getOrDefault(c,0) +1);
            }
            boolean isAns = true;
            for (int i = 0; i < wordlen ; i++) {
                char c = word.charAt(i);
                if(charCnt.getOrDefault(c,0) < wordCnt.getOrDefault(c,0)){
                    isAns = false;
                    break;
                }
            }
            if(isAns){
                ans += word.length();
            }
        }
        return ans;
    }
    public static void main2(String[] args) {
        String[] words = {"cat","bt","hat","tree"};
        String chars = "atach";
        int ret = countCharacters(words,chars);
        System.out.println(ret);
    }
    public static int stone(int[] arry){
        int len = arry.length;
        int right = len - 1;
        for (int i = 0; i < right; i++) {
            Arrays.sort(arry);
            if(arry[right - 1] == 0){
                break;
            }
            arry[right] -= arry[right - 1];
            arry[right - 1] = 0;
        }
        return arry[right];
    }
    public static void main1(String[] args) {
        int[] arry = {2,7,4,1,8,1,1};
        int ret = stone(arry);
        System.out.println(ret);
    }
}
